/**
 * ...
 * >author		João Vítor de Souza
 */

package com.cupcakeengine.display 
{
	import com.cupcakeengine.core.IDestroyable;
	import com.cupcakeengine.utils.MathUtil;
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.System;
	import flash.utils.Dictionary;

	public class BitmapObject implements IDestroyable
	{
		// ___________________________________________________________________ CONSTANTS		
		
		// ___________________________________________________________________ CLASS PROPERTIES	
		
		private var _dBitmapData						: Dictionary;		
		private var _currentVector						: Vector.<BitmapData>;		
		private var _label								: String;		
		
		protected var _rectangle						: Rectangle;
		protected var _point							: Point;		
		protected var _alpha							: BitmapData;
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		
		protected var _bitmapData						: BitmapData;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		
		public function get bitmapData():BitmapData { return _bitmapData; }
		
		public function set bitmapData(value:BitmapData):void 
		{
			_bitmapData = value;
		}
		
		public function get rectangle():Rectangle { return _rectangle; }
		
		public function get point():Point { return _point; }
		
		public function set point(value:Point):void 
		{
			_point = value;
		}
		
		public function get alpha():BitmapData { return _alpha; }		
		
		public function get currentVector():Vector.<BitmapData> { return _currentVector; }
		
		public function set currentVector(value:Vector.<BitmapData>):void 
		{
			_currentVector = value;
		}		
		
		public function get dBitmapData():Dictionary { return _dBitmapData; }		
		
		public function get label():String { return _label; }
		
		// ___________________________________________________________________ CONSTRUCTOR		
		
		public function BitmapObject(bitmapData:BitmapData, rectangle:Rectangle, dBitmapData:Dictionary = null, currentVector:Vector.<BitmapData> = null, label:String = null)
		{			
			_bitmapData = bitmapData;
			_rectangle = rectangle;
			_point = new Point();
			
			_dBitmapData = dBitmapData;
			_currentVector = currentVector;
			_label = label;			
		}
		
		public function refreshBitmapData(bitmapData:BitmapData, point:Point):void
		{
			_bitmapData = bitmapData;
			_point = point;
		}
		
		// ___________________________________________________________________ PUBLIC METHODS
		
		
		public function setAlpha(value:Number):void 
		{
			if (value >= 1) {
				_alpha = null;
				return;
			}
			
			_alpha = new BitmapData(_bitmapData.width, _bitmapData.height, true, int('0x'+(value * 255).toString(16)+'FFFFFF'));
		}
		
		public function setRotation(value:Number):void
		{			
			var angle:Number = MathUtil.d2r(value);			
			
			var width:Number = _bitmapData.width >> 1;
			var height:Number = _bitmapData.height >> 1;
			
			var matrix:Matrix = new Matrix();
            matrix.translate(-width,-height);
            matrix.rotate(angle);
            matrix.translate(width, height);
			
			setMatrix(matrix, _bitmapData.width, _bitmapData.height);
		}
		
		public function setScale(sx:Number, sy:Number):void
		{
			var width:Number = _bitmapData.width >> 1;
			var height:Number = _bitmapData.height >> 1;
			
			var matrix:Matrix = new Matrix();			
			matrix.translate(-width,-height);
			matrix.scale(sx, sy);			
			matrix.translate(width, height);
			
			setMatrix(matrix, _bitmapData.width * MathUtil.abs(sx), _bitmapData.height * MathUtil.abs(sy));
		}		
		
		/* INTERFACE com.cupcakeengine.core.IDestroyable */
		
		public function destroy():void
		{
			if (_dBitmapData != null) {
				var len:int;				
				for each(var v:Vector.<BitmapData> in _dBitmapData) {
					len = v.length;
					for (var i:int = 0; i < len; i++) {
						v[i].dispose();
						v[i] = null;
					}
					v = null;
				}
			}
			
			if(_bitmapData != null) _bitmapData.dispose();
			_bitmapData = null;
			
			if (_alpha != null) _alpha.dispose();
			_alpha = null;
		}		
		
		// ___________________________________________________________________ PRIVATE METHODS
		
		protected function setMatrix(matrix:Matrix, width:Number, height:Number):void
		{	
			var draw:BitmapData = _bitmapData.clone();			
			_bitmapData = new BitmapData(width, height, true, 0);
			_bitmapData.draw(draw, matrix, null,  null, null, true);
			
			if (_dBitmapData != null) {
				var len:int;				
				for each(var v:Vector.<BitmapData> in _dBitmapData) {
					len = v.length;
					for (var i:int = 0; i < len; i++) {					
						draw = v[i].clone();
						_bitmapData = new BitmapData(width, height, true, 0);
						_bitmapData.draw(draw, matrix, null,  null, null, true);
						v[i] = bitmapData;					
					}
				}
			}
			
			draw = null;
			System.gc();
		}
		
		// ___________________________________________________________________ EVENTS
	}
}

